package com.homwee.factory.util;

import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.Hashtable;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.PixelFormat;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.ImageView;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;

public class BarCodeUtils {
	private static int QR_WIDTH = 200, QR_HEIGHT = 200;
	private static ImageView mView;
	private static WindowManager mWindowManager;
	private static String PREFERENCES_WIFI_MAC_ADDRESS= "tv-wifimac-address";
	private static String KEY_WIFI_MAC_ADDRESS="wifi_mac_address";

	public static Bitmap createQRImage(String url)
	{
		Bitmap bitmap = Bitmap.createBitmap(QR_WIDTH, QR_HEIGHT, Bitmap.Config.ARGB_8888);
		try
		{
			if (url == null || "".equals(url) || url.length() < 1)
			{
				return null;
			}
			Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
			hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
			BitMatrix bitMatrix = new QRCodeWriter().encode(url, BarcodeFormat.QR_CODE, QR_WIDTH, QR_HEIGHT, hints);
			int[] pixels = new int[QR_WIDTH * QR_HEIGHT];
			for (int y = 0; y < QR_HEIGHT; y++)
			{
				for (int x = 0; x < QR_WIDTH; x++)
				{
					if (bitMatrix.get(x, y))
					{
						pixels[y * QR_WIDTH + x] = 0xff000000;
					}
					else
					{
						pixels[y * QR_WIDTH + x] = 0xffffffff;
					}
				}
			}
			bitmap.setPixels(pixels, 0, QR_WIDTH, 0, 0, QR_WIDTH, QR_HEIGHT);
		}
		catch (WriterException e)
		{
			e.printStackTrace();
			return null;
		}
		return bitmap;
	}
	
	 /** 
     *
     * 
     * @param context 
     * @param view 
     */  
    public static void showPopupWindow(final Context context, final Bitmap bitmap, final WindowManager.LayoutParams windowParams) {  
    	Context mContext = context.getApplicationContext();  
    	mWindowManager = (WindowManager) mContext
                .getSystemService(Context.WINDOW_SERVICE);
    	if(mView==null) {
			mView = new ImageView(mContext);
		}
        mView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
        mView.setImageBitmap(bitmap);

        mWindowManager.addView(mView, windowParams);
    }

    public static  void removePopupWindow(){
		if(mView!=null) {
			mWindowManager.removeView(mView);
		}
	}
    
    public static String getMAC(String str) {
		Enumeration<NetworkInterface> interfaces = null;
		try {
			interfaces = NetworkInterface.getNetworkInterfaces();
		} catch (SocketException e) {
			e.printStackTrace();
		}
		String hardWareAddress = null;
		NetworkInterface iF = null;
		if (interfaces == null) {
			return null;
		}
		while (interfaces.hasMoreElements()) {
			iF = interfaces.nextElement();
			if (iF.getDisplayName().equalsIgnoreCase(str)) {
				try {
					hardWareAddress = bytesToString(iF.getHardwareAddress());
					if (hardWareAddress != null)
						break;
				} catch (SocketException e) {
					e.printStackTrace();
				}
			} else {
				hardWareAddress = "";
			}
		}
		return hardWareAddress;

	}

	/**
	 * get deviceMac
	 */
	public static String getDeviceMac(Context context){
		String KEY_WIFI_MAC_ADDRESS="wifi_mac_address";
		SharedPreferences sharedPrefs = context.getSharedPreferences(PREFERENCES_WIFI_MAC_ADDRESS, Context.MODE_PRIVATE);
		String deviceMac=sharedPrefs.getString(KEY_WIFI_MAC_ADDRESS," ");
		return deviceMac;
	}

    
    /**
	 * 
	 * @param bytes
	 * @return
	 */
	private static String bytesToString(byte[] bytes) {
		if (bytes == null || bytes.length == 0) {
			return null;
		}
		StringBuilder buf = new StringBuilder();
		for (byte b : bytes) {
			buf.append(String.format("%02X:", b));
		}
		if (buf.length() > 0) {
			buf.deleteCharAt(buf.length() - 1);
		}
		return buf.toString();
	}
}
